package cn.damai.user.service.impl;

import cn.damai.common.MD5;
import cn.damai.model.User;
import cn.damai.user.mapper.UserMapper;


import cn.damai.user.service.UserService;
import cn.damai.util.DubboUtil;

import cn.damai.util.PasswordEncrypt;
import com.alibaba.dubbo.config.annotation.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;


@Component
@Service(version = DubboUtil.VRESION,group = DubboUtil.GROUP)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean checkPhoneExists(String phone) {
        if(phone ==null){
            return false;
        }
        phone=phone.trim();
        if(phone.equals("")){
            return false;
        }
        int row=userMapper.checkPhoneExists(phone);
        if(row >= 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public User queryByPhone(String phone) {
        return userMapper.queryByPhone(phone);
    }

    @Override
    public int register(User user) {
        user.setCreatedTime(new Date());
        user.setNickName("麦子"+001);
//        MD5 m =new MD5();
//        String newPassword=m.md5(user.getPassword());

        if (user.getPassword()!=null && !user.getPassword().equals("")){
            String psEnc = PasswordEncrypt.createPassword(user.getPassword());

            user.setPassword(psEnc);
            return userMapper.insert(user);
        }else {
            return 0;
        }

    }



}
